package com.glory.dgcpcpda.activity.technicalservice;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.RepairLogListAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.base.Constant;
import com.glory.dgcpcpda.entity.RepairFromEntity;
import com.glory.dgcpcpda.entity.RepairFromListEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class QCConfirmActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvEqp)
    TextView tvEqp;
    @BindView(R.id.tvLocation)
    TextView tvLocation;
    @BindView(R.id.tvRepairType)
    TextView tvRepairType;
    @BindView(R.id.tvPriority)
    TextView tvPriority;
    @BindView(R.id.tvLotID)
    TextView tvLotID;
    @BindView(R.id.rvFaultLog)
    RecyclerView rvFaultLog;
    @BindView(R.id.tvQualified)
    TextView tvQualified;
    @BindView(R.id.tvUnQualified)
    TextView tvUnQualified;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etComment)
    EditText etComment;
    private RepairFromListEntity.RepairData mData;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_q_c_confirm;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("QC检验");
        tvBaseSubmit.setText("维修详情");
        Bundle bundle = getIntent().getExtras();
        mData = (RepairFromListEntity.RepairData) bundle.getSerializable("REPAIRFROM");
        tvEqp.setText(mData.getREPAIRSHEETDATA().getEQUIPMENTID());
        tvLocation.setText(mData.getREPAIRSHEETDATA().getLOCATION());
        tvRepairType.setText(getType(mData.getREPAIRSHEETDATA().getREPAIRTYPE()));
        tvPriority.setText(mData.getREPAIRSHEETDATA().getPRIORITY());
        tvLotID.setText(mData.getREPAIRSHEETDATA().getLOTID());
        initRecyclerView(rvFaultLog, this);
        RepairLogListAdapter mRepairLogAdapter = new RepairLogListAdapter(mData.getREPAIRFORMHISLIST());
        mRepairLogAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mRepairLogAdapter.isFirstOnly(true);
        rvFaultLog.setAdapter(mRepairLogAdapter);
    }

    private String getType(String repairtype) {
        String type = "";
        switch (repairtype) {
            case "normal":
                type = "一般故障";
                break;
            case "eqp":
                type = "设备故障";
                break;
            case "program":
                type = "工艺问题";
                break;
            case "changeProducct":
                type = "换产品";
                break;
            case "changeMaterial":
                type = "换材料";
                break;
            case "changeTool":
                type = "换Tooling";
                break;
            case "Testprogram":
                type = "程序问题";
                break;
            default:
                type = "未知错误";
                break;
        }
        return type;
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvQualified, R.id.tvUnQualified,R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvQualified:
                showSubmitDialog(() -> requestQC("OK"));
                break;
            case R.id.tvUnQualified:
                showSubmitDialog(() -> requestQC("NG"));
                break;
            case R.id.tvBaseSubmit:
                showActivity();
                break;
        }
    }

    private void showActivity() {
        Bundle bundle = new Bundle();
        bundle.putString("FROM","QC");
        bundle.putSerializable("REPAIRFROM",mData);
        switch (tvRepairType.getText().toString()){
            case "一般故障":
            case "设备故障":
            case "程序问题":
            case "工艺问题":
                FreeApi_Utils.skipWithExtraIntent("header",bundle,this,FaultRegisterActivity.class);
                break;
            case "换产品":
                FreeApi_Utils.skipWithExtraIntent("header",bundle,this,ReplaceProductActivity.class);
                break;
            case "换材料":
                FreeApi_Utils.skipWithExtraIntent("header",bundle,this,ReplaceMaterialActivity.class);
                break;
            case "换Tooling":
                FreeApi_Utils.skipWithExtraIntent("header",bundle,this,ReplaceToolActivity.class);
                break;
        }
    }

    private void requestQC(String result) {
        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    //showConfirmDialog(QCConfirmActivity.this, "提交成功");
                    showConfirmDialog(QCConfirmActivity.this,  response.body().getResponse().getHeader().getRESULTMESSAGE());


                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(), "PMSREPAIR");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","QC");
        map.addProperty("COMMENTS",getText(etComment));
        map.addProperty("REPAIRTYPE",mData.getREPAIRSHEETDATA().getREPAIRTYPE());
        map.addProperty("QC","Y");
        map.addProperty("QCRESULT",result);
        map.add("REPAIRFORM", new JsonParser().parse(new Gson().toJson(mData.getREPAIRFORM())).getAsJsonObject());
        map.add("REPAIRSHEETDATA", new JsonParser().parse(new Gson().toJson(mData.getREPAIRSHEETDATA())).getAsJsonObject());
        NetUtils.mRequestPostNet("CPC.PMSREPAIR", mData.getName(),"PMSREPAIR", this, map, callBack);
    }
}